﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Threading;
using EveryBookies.ViewModel;
using EveryBookies.Model;
using EveryBookies.Common;
using Microsoft.Practices.ServiceLocation;
using EveryBookies.View;

namespace EveryBookies.Controls
{
    public partial class OfferUserControl : UserControl
    {
        DispatcherTimer _timer = new DispatcherTimer();
        DataSave offer = null;
        public BookiesViewModel BookiesViewModelData
        {
            get
            {
                return (BookiesViewModel)DataContext;
            }
        }
        public ILocalData SaveDataWeb
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ILocalData>();
            }
        }
        bool isSave = true;
        bool isBanner = false;
        public OfferUserControl()
        {
            InitializeComponent();
            this.Loaded += OfferPage_Loaded;
        }

        public bool CloseWebView()
        {
            BookiesViewModelData.Offers.Clear();
            BookiesViewModelData.Banners.Clear();
            BookiesViewModelData.GetOffer(App.ItemsCountDefault.ToString(), "0");
            if (showWebViewOffer.Visibility == System.Windows.Visibility.Visible)
            {
                homeBT.Visibility = System.Windows.Visibility.Collapsed;
                showWebViewOffer.Visibility = System.Windows.Visibility.Collapsed;
                showWebViewOffer.NavigateToString("");
                BookiesViewModelData.IsShowOfferLoading = false;
                btBack.Visibility = System.Windows.Visibility.Collapsed;
                isSave = false;
                return true;
            }

            return false;
        }

        void OfferPage_Loaded(object sender, RoutedEventArgs e)
        {
            _timer.Interval = TimeSpan.FromSeconds(8.0);
            _timer.Tick += ((senders, es) =>
            {
                if (BookiesViewModelData.Banners.Count > 0)
                {
                    if (loadBanner.SelectedIndex < loadBanner.Items.Count - 1)
                        loadBanner.SelectedIndex++;
                    else
                        loadBanner.SelectedIndex = 0;
                }
            });
            _timer.Start();

        }
        private async void LongListSelector_ItemRealized(object sender, ItemRealizationEventArgs e)
        {
            if (resultListBox.ItemsSource != null && resultListBox.ItemsSource.Count >= 1)
            {
                if (e.ItemKind == LongListSelectorItemKind.Item)
                {
                    var eco = (Offer)e.Container.Content;
                    var itemlis = resultListBox.ItemsSource[resultListBox.ItemsSource.Count - 1];
                    if (eco.Equals(itemlis))
                    {
                        var startIndex = resultListBox.ItemsSource.Count;
                        await BookiesViewModelData.GetOffer(App.ItemsCountDefault.ToString(), startIndex.ToString());
                    }
                }
            }
        }

        private void loadComplete(object sender, NavigationEventArgs e)
        {
            if (isSave)
            {
                var getURi = e.Uri;
                if (!isBanner)
                {
                    SaveDataWeb.SaveData(new DataSave() { id = offer.id, link = getURi.ToString() }, string.Format("offerId{0}", offer.id));
                }
                else
                {
                    SaveDataWeb.SaveData(new DataSave() { id = offer.id, link = getURi.ToString() }, string.Format("BannerId{0}", offer.id));
                }
                BookiesViewModelData.IsShowOfferLoading = false;
                isBanner = false;
            }

        }

        private void startLoad(object sender, NavigatingEventArgs e)
        {
            BookiesViewModelData.IsShowOfferLoading = true;
        }

        private async void selectBanner(object sender, System.Windows.Input.GestureEventArgs e)
        {

            var getData = (Pivot)sender;
            try
            {
                var data = getData.Items[getData.SelectedIndex] as Banner;
                homeBT.Visibility = System.Windows.Visibility.Visible;
                btBack.Visibility = System.Windows.Visibility.Visible;
                isSave = true;
                isBanner = true;
                BookiesViewModelData.IsShowOfferLoading = true;
              
                if (data != null)
                {
                    offer = new DataSave() { id = data.id, link = data.link };
                    var getDataSave = await SaveDataWeb.LoadData<DataSave>(string.Format("BannerId{0}", offer.id));
                    if (getDataSave != null)
                    {
                        if (!string.IsNullOrEmpty(getDataSave.link))
                        {
                            showWebViewOffer.Source = new Uri(getDataSave.link);
                        }
                        else
                        {
                            showWebViewOffer.Source = new Uri(offer.link);
                        }
                    }
                    else
                    {
                        showWebViewOffer.Source = new Uri(offer.link);
                    }
                    isSave = true;
                    showWebViewOffer.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
                BookiesViewModelData.IsShowOfferLoading = false;
            }
           
        }

        private async void selectItemOffer(object sender, System.Windows.Input.GestureEventArgs e)
        {
            homeBT.Visibility = System.Windows.Visibility.Visible;
            btBack.Visibility = System.Windows.Visibility.Visible;
            BookiesViewModelData.IsShowOfferLoading = true;
            var getData = ((Grid)sender).DataContext as Offer;
            if (getData != null)
            {
                offer = new DataSave() { id = getData.id, link = getData.link };

                var getDataSave = await SaveDataWeb.LoadData<DataSave>(string.Format("offerId{0}", offer.id));
                if (getDataSave != null)
                {
                    if (!string.IsNullOrEmpty(getDataSave.link))
                    {
                        showWebViewOffer.Source = new Uri(getDataSave.link);
                    }
                    else
                    {
                        showWebViewOffer.Source = new Uri(offer.link);
                    }
                }
                else
                {
                    showWebViewOffer.Source = new Uri(offer.link);
                }
                isSave = true;
                showWebViewOffer.Visibility = Visibility.Visible;
            }

        }

        private void showWebViewOffer_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            BookiesViewModelData.IsShowOfferLoading = false;
        }
        private void backbt(object sender, RoutedEventArgs e)
        {
            CloseWebView();



        }

        private void backHome(object sender, RoutedEventArgs e)
        {
            CloseWebView();
        }
    }
}
